import { request } from '@/utils/request'

// 认证相关API接口类型定义
export interface LoginRequest {
  email: string
  password: string
  remember?: boolean
}

export interface RegisterRequest {
  username: string
  email: string
  realName?: string
  password: string
  confirmPassword: string
}

export interface ForgotPasswordRequest {
  email: string
}

export interface VerifyResetCodeRequest {
  email: string
  code: string
}

export interface ResetPasswordRequest {
  email: string
  code: string
  password: string
}

export interface AuthResponse {
  token: string
  user: {
    id: string
    username: string
    email: string
    realName?: string
    avatar?: string
  }
}

// 认证API类
export class AuthAPI {
  /**
   * 用户登录
   */
  static async login(data: LoginRequest): Promise<AuthResponse> {
    return request.post('/auth/login', data)
  }

  /**
   * 用户注册
   */
  static async register(data: RegisterRequest): Promise<AuthResponse> {
    return request.post('/auth/register', data)
  }

  /**
   * 用户登出
   */
  static async logout(): Promise<void> {
    return request.post('/auth/logout')
  }

  /**
   * 刷新Token
   */
  static async refreshToken(): Promise<{ token: string }> {
    return request.post('/auth/refresh')
  }

  /**
   * 发送密码重置验证码
   */
  static async sendResetCode(data: ForgotPasswordRequest): Promise<{ message: string }> {
    return request.post('/auth/forgot-password', data)
  }

  /**
   * 验证密码重置验证码
   */
  static async verifyResetCode(data: VerifyResetCodeRequest): Promise<{ message: string }> {
    return request.post('/auth/verify-reset-code', data)
  }

  /**
   * 重置密码
   */
  static async resetPassword(data: ResetPasswordRequest): Promise<{ message: string }> {
    return request.post('/auth/reset-password', data)
  }

  /**
   * 重新发送验证码
   */
  static async resendResetCode(data: ForgotPasswordRequest): Promise<{ message: string }> {
    return request.post('/auth/resend-reset-code', data)
  }

  /**
   * 获取当前用户信息
   */
  static async getCurrentUser(): Promise<AuthResponse['user']> {
    return request.get('/auth/me')
  }

  /**
   * 检查邮箱是否已注册
   */
  static async checkEmail(email: string): Promise<{ exists: boolean }> {
    return request.get(`/auth/check-email?email=${encodeURIComponent(email)}`)
  }

  /**
   * 检查用户名是否已存在
   */
  static async checkUsername(username: string): Promise<{ exists: boolean }> {
    return request.get(`/auth/check-username?username=${encodeURIComponent(username)}`)
  }
}

// 导出默认实例
export default AuthAPI
